<?php

namespace app\index\controller;

use app\common\controller\HomeBase;
use think\Db;
use org\Sitemap;

class Index extends HomeBase {

    
    public function index() {
        //最新的电影
        $data['yun'] = getChildMovies(71, 'add_time desc',6)['data'];
        $data['guo'] = getChildMovies(68, 'add_time desc',6)['data'];
        $data['ya'] = getChildMovies(70, 'add_time desc',6)['data'];
        $data['ou'] = getChildMovies(69, 'add_time desc',6)['data'];

        //点击率最高
        $ranking['yun'] = getChildMovies(71,'click desc',8)['data'];
        $ranking['guo'] = getChildMovies(68,'click desc',8)['data'];
        $ranking['ya'] = getChildMovies(70,'click desc',8)['data'];
        $ranking['ou'] = getChildMovies(69,'click desc',8)['data'];
        
        //最新资讯
        $new_nws = Db::name('Article')->cache(3600)->field('id,title,introduction,reading,publish_time')->order('publish_time desc')->paginate(10);
        
        return $this->fetch('',['data'=>$data,'ranking'=>$ranking,'new_nws'=>$new_nws]);
    }
    
    /**
     *  生成Sitemap.xml文件
     */
    public function  sitemapXml() {
       
        $site = new Sitemap();
        // 取出顶级导航
        $data = $nav = Db::name('category')->field('id,name,pingying')->where(['is_nav'=>1,'pid'=>0])->order('sort asc')->select();
        if ($data) {
            // 循环每个顶级取二级
            foreach ($data as $k => $v) {
                $data[$k]['children'] = Db::name('category')->field('id,name,pingying')->where(['pid'=>$v['id'],'is_nav'=>1])->order('sort asc')->select();
            }
        }
        
        foreach ($data as $v)
        {
            if($v['children']){
                foreach ($v['children'] as $vv){
                     //二级栏目
                    $site->AddItem(request()->domain().'/'.$vv['pingying'].'.txt', 2);
                    
                    //获取该栏目下的所有文章
                    $movies = Db::name('Movies')->field('id')->where(['pid'=>$vv['id']])->select();
                   
                    $movies_url = '';
                        foreach ($movies as $kkk=>$vvv){
                            $movies_url.=request()->domain().url('/'.$vvv['id'])."\r\n";
                        }
                        file_put_contents($vv['pingying'].'.txt', $movies_url);
                }
            }else{
                //顶级栏目
                $site->AddItem(request()->domain().'/'.$v['pingying'].'.txt', 1);
                $ids ='';
                foreach (cache('Category_list') as $kk=>$vv) {
                    if($vv['pid'] == $v['id']){
                       $ids.=$vv['id'].','; 
                    }
                }
                
                //获取该栏目下的所有文章
                $movies = Db::name('Movies')->field('id')->where(['pid'=>['in',$ids]])->select();

                $movies_url = '';
                foreach ($movies as $kkk=>$vvv){
                    $movies_url.=request()->domain().url('/'.$vvv['id'])."\r\n";
                }
                file_put_contents($v['pingying'].'.txt', $movies_url);
                
            }
        }
        
        //资讯所有文章
        $article_list = Db::name('Article')->field('id')->select();
        $links = '';
        if($article_list){
            foreach ($article_list as $k=>$v){
                $links .= request()->domain().url('/article/'.$v['id'])."\r\n";
            }
        }
        $site->AddItem(request()->domain().'/zixun.txt', 1);
        file_put_contents('zixun'.'.txt', $links);
        
        $site->SaveToFile('sitemap.xml');
        echo '完成';
        
    }
    
    
    /*网站地图*/
    public function sitemapHtml () {
        
        // 取出顶级导航
        $data = $nav = Db::name('category')->field('id,name,pingying')->where(['is_nav'=>1,'pid'=>0])->order('sort asc')->select();
        if ($data) {
            // 循环每个顶级取二级
            foreach ($data as $k => $v) {
                $data[$k]['children'] = Db::name('category')->field('id,name,pingying')->where(['pid'=>$v['id']])->order('sort asc')->select();
            }
        }
        $this->assign(['data'=>$data,'title'=>'网站地图']);
        
        return  $this->fetch('sitemaphtml');
    }
    
    
}
